import socket

# 获取所有本机ip
hosts = socket.gethostbyname_ex(socket.gethostname())[2]
# 按指定要求过滤
hosts_filter = [host for host in hosts if host.startswith("192.168") or host.startswith("172.16")]

print(hosts_filter)